//
//  CXHomeTableViewCell.swift
//  CXSwiftDemo
//
//  Created by 潮汐 on 2023/11/29.
//

import UIKit
//图片解析
import Kingfisher

class CXHomeTableViewCell: UITableViewCell {
    //数据模型
    var modelCell: CXHomeListModel? = nil
    var titleLabel = UILabel()//标题
    var contentLabel = UILabel()//描述
    var imageViewLogo = UIImageView()//图片
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
        
    }
    //
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        fatalError("init(coder:) has not been implemented")
    }
    //初始化方法，在这里创建UI
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.white//背景色
        //创建UI
        createUI()
    }
    
    //创建UI
    func createUI(){
        //图片
        imageViewLogo.frame = CGRect(x: 15, y: 10, width:70, height: 70)
        imageViewLogo.layer.cornerRadius = 5
        imageViewLogo.layer.masksToBounds = true
        self.addSubview(imageViewLogo)
        
        //标题
        titleLabel.frame = CGRect(x: 100, y: 10, width: UIScreen.main.bounds.size.width, height: 30)
        titleLabel.font = .systemFont(ofSize: 18)
        titleLabel.textColor = .black
        titleLabel.text = ""
        self.addSubview(titleLabel)
        
        //描述
        contentLabel.frame = CGRect(x: 100, y: 50, width: UIScreen.main.bounds.size.width, height: 30)
        contentLabel.font = .systemFont(ofSize: 15)
        contentLabel.textColor = .gray
        contentLabel.text = ""
        self.addSubview(contentLabel)
        
        
        
    }
    
    //cell赋值
    func addDataToUI(model:CXHomeListModel){
        modelCell = model
        //标题
        titleLabel.text = modelCell?.name
        //描述
        contentLabel.text = modelCell?.content
        //图片赋值
        //为了简便，这里把webp格式的图片转为png图片
        let url = modelCell?.thumb.replacingOccurrences(of: ".webp", with: ".png") ?? ""
        //加载图片
        imageViewLogo.kf.setImage(with: URL(string: url))
        //Kingfisher是不支持WebP格式的图片，需要用pod下载KingfisherWebP三方，但是KingfisherWebP下载的同时需要下载libwebp，libweb在googlecode.com/xxx，正常网速下载不下来
        
    }
}
